package com.bianxianmao.lastknow.fragment;

import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bianxianmao.R;
import com.bianxianmao.base.BaseFragment;
import com.bianxianmao.lastknow.adapter.MyListViewAdapter;
import com.bianxianmao.lastknow.bean.LastKnowBean;
import com.bianxianmao.utils.ToastUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/7/7 0007.
 */

public class LastkowFragment extends BaseFragment {

    private TextView tvLastknow;
    private View view1;
    private TextView tvMy;
    private View view2;
    private PullToRefreshListView pullListview;
    private ProgressBar pb;
    private List<LastKnowBean.ReturnValueBean> datalist = new ArrayList();
    private MyListViewAdapter myAdapter;
    public String URLHEAD = "http://duobao.bianxianmao.com/duobao/period/getPeroidListByLastAnnounApi.json?appKey=oWc0jGVZn9Aqo5uXN4duvwbQqHQ1Gamk&page_index=";
    private int page_index = 0;
    public String URLEND = "&page_size=6&noCache3=6221502688965856";
    public String URL = URLHEAD + page_index + URLEND;

    @Override
    public View initView() {
        initData();
        initEvent();
        View view = View.inflate(mContext, R.layout.fragment_lastknow, null);
        tvLastknow = (TextView) view.findViewById(R.id.tv_lastknow);
        view1 = view.findViewById(R.id.view1);
        tvMy = (TextView) view.findViewById(R.id.tv_my);
        view2 = view.findViewById(R.id.view2);
        pullListview = (PullToRefreshListView) view.findViewById(R.id.pull_listview);
        pb = (ProgressBar) view.findViewById(R.id.pb);
        pullListview.setEmptyView(pb);

        myAdapter = new MyListViewAdapter(datalist, mContext);
        pullListview.setAdapter(myAdapter);

//        vp = (ViewPager) view.findViewById(vp);
//        fragments= new ArrayList<BaseFragment>();
//        fragments.add(new NewFragment());
//        fragments.add(new Myfragment());
//        lastknowAdapter = new LastknowAdapter(getChildFragmentManager(),fragments,titles);
//        vp.setAdapter(lastknowAdapter);
//
//
//        tab = (TabLayout) view.findViewById(tab);
////        tab.addTab(tab.newTab().setText(titles[0]));
////        tab.addTab(tab.newTab().setText(titles[1]));
//        tab.setupWithViewPager(vp);
//        tab.setTabMode(TabLayout.MODE_FIXED);
//
//        tab.post(new Runnable() {
//            @Override
//            public void run() {
//                setIndicator(tab, 30, 30);
//            }
//        });

        return view;
    }

    @Override
    public void initData() {


        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils.get().url(URL).build().execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Log.i("AACC", "onError: " + request);
                    }

                    @Override
                    public void onResponse(String response) {
                        LastKnowBean lastKnowBean = JSON.parseObject(response, LastKnowBean.class);
                        Log.i("DDDDDD", "onResponse: " + lastKnowBean.getReturnValue().get(0).getDbOpenTime());
                        datalist.addAll(lastKnowBean.getReturnValue());
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                myAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                });
            }
        }).start();
    }

    private void initEvent() {
        pullListview.setOnPullEventListener(new PullToRefreshBase.OnPullEventListener<ListView>() {
            @Override
            public void onPullEvent(PullToRefreshBase<ListView> refreshView, PullToRefreshBase.State state, PullToRefreshBase.Mode direction) {
                ToastUtil.show(getContext(), "加载完成", Toast.LENGTH_SHORT);
            }
        });
        pullListview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    // 当不滚动时
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        // 判断滚动到底部
                        if (view.getLastVisiblePosition() == (view.getChildCount() - 1)) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    requestfornet();

                                }
                            }).start();
                        }
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
//        tvLastknow.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                tvLastknow.setTextColor(R.color.textColor_selected);
//                view1.setBackgroundColor(R.color.textColor_selected);
//                tvMy.setTextColor(R.color.textColor_unselected);
//                view2.setBackgroundColor(Color.WHITE);
//            }
//        });
//        tvMy.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                tvMy.setTextColor(R.color.textColor_selected);
//                view2.setBackgroundColor(R.color.textColor_selected);
//                tvLastknow.setTextColor(R.color.textColor_unselected);
//                view1.setBackgroundColor(Color.WHITE);
//            }
//        });

    }

    private void requestfornet() {
        page_index = page_index + 1;
        URL = URLHEAD + page_index + URLEND;
        OkHttpUtils.get().url(URL).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                LastKnowBean lastKnowBean = JSON.parseObject(response, LastKnowBean.class);
                datalist.addAll(lastKnowBean.getReturnValue());
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myAdapter.notifyDataSetChanged();
                    }
                });

            }
        });

    }
}
